package com.quan.WorkTest;

import com.quan.Config.MQConnection;
import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;

public class comsumer2 {
    @Test
    public void cumsumer() throws Exception{
        //创建连接
        Connection conn = MQConnection.getRabbitmaConection();

        //创建channel
        final Channel channel = conn.createChannel();

        //生命队列
        channel.queueDeclare("Work",true,false,false,null);

        //指定当前消费者一次消费多少消息
        channel.basicQos(1);

        //开启监听
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                try{
                    Thread.sleep(200);
                }catch (Exception e){

                }
                System.out.println("消费者2号接收到了消息" + new String(body,"UTF-8"));
                //消费完后,手动ack
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };

        channel.basicConsume("Work",false,defaultConsumer);


        //卡住
        System.out.println("消费者2号开始监听队列");
        System.in.read();

        //关闭资源
        channel.close();
        conn.close();
    }
}
